<?php

namespace App\Http\Controllers\CRM;

use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\CRM\LoginRequest;
use App\Models\User;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function login(LoginRequest $request)
    {
        $user = User::withTelephone($request->telephone)->first();

        try {
            $token = $user->login($request->password);

            return ApiResponseHelper::json([
                'token' => $token->plainTextToken,
            ]);
        } catch (\Exception $e) {
            return ApiResponseHelper::json([], 400, $e->getMessage());
        }
    }
}
